package com.groot.medical.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.AdministrativeArea;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.Ztree;
import com.groot.medical.service.AdministrativeAreaService;
import com.groot.medical.service.AdministrativeAreaServiceImpl;
@WebServlet("/sys/area.do")
public class AdministrativeAreaController extends BaseServletController {
	
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	private AdministrativeAreaService areaService = new AdministrativeAreaServiceImpl();
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 分页查询行政区域列表信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAreaByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 5;
		PageBean areaPage = areaService.findByPage(pageNum, pageSize);
		System.out.println("areaPage:"+areaPage);
		request.setAttribute("areaPage", areaPage);
		return "/WEB-INF/page/area/area_list.jsp";
		
	}
	/**
	 * 打开行政地区添加页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openAddArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		//查询行政地区信息形成树信息
		List<AdministrativeArea> areaList = areaService.findAllAreaInfo();
		//逐个遍历封装进ztree
		List<Ztree> ztrees = new ArrayList<>();
		for(int i=0;i<areaList.size();i++){
			AdministrativeArea area = areaList.get(i);
			Ztree ztree = new Ztree();
			ztree.setId(String.valueOf(area.getId()));
			ztree.setpId(String.valueOf(area.getPid()));
			ztree.setName(area.getAreaName());
			ztrees.add(ztree);
			
		}
		Gson gson = new Gson();
		String json = gson.toJson(ztrees);
		request.setAttribute("areaList", json);
		
		return "/WEB-INF/page/area/area_add.jsp";
	}
	/**
	 * 添加行政区域
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addAreaInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pid = request.getParameter("pid");
		String areaname = request.getParameter("areaname");
		
		String tgId = request.getParameter("tgId");
		String pageNum = request.getParameter("pageNum");
		
		AdministrativeArea area = new AdministrativeArea();
		area.setPid(Integer.parseInt(pid));
		area.setAreaName(areaname);
		//添加行政区域
		int i = areaService.addAreaInfo(area);
		if(i==1){
			request.setAttribute("msg", "<script>alert('修改成功')</script>");
			return findAreaByPage(request,response);
		
		}else
			LOGGER.info("添加失败"+i);
		
		return null;
	}
	/**
	 * 根据县级行政区域编码查询乡镇信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findTownInfoByCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String areacode=request.getParameter("areacode");
		//根据县级行政区域编码查询乡镇信息
		List<AdministrativeArea> townArea = areaService.findTownAreaInfo(areacode);
		System.out.println("急啊急啊急啊急啊急啊就："+townArea);
		PrintWriter pw = response.getWriter();
		try {
			Gson g = new Gson();
			String json = g.toJson(townArea);
			pw.print(json);
			pw.flush();
		} finally {
			pw.close();
		}
		return null;
	}
	/**
	 * 根据乡镇级行政区域编码查询村信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findVillageInfoByCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String areacode=request.getParameter("areacode");
		List<AdministrativeArea> villageArea = areaService.findVillageAreaInfo(areacode);
		System.out.println("急啊急啊急啊急啊急啊就："+villageArea);
		PrintWriter pw = response.getWriter();
		try {
			Gson g = new Gson();
			String json = g.toJson(villageArea);
			pw.print(json);
			pw.flush();
		} finally {
			pw.close();
		}
		return null;
	}
	/**
	 * 根据村编码查询组信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findGroupInfoByCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String areacode=request.getParameter("areacode");
		List<AdministrativeArea> gruopArea = areaService.findGroupAreaInfo(areacode);
		System.out.println("急啊急啊急啊急啊急啊就："+gruopArea);
		PrintWriter pw = response.getWriter();
		try {
			Gson g = new Gson();
			String json = g.toJson(gruopArea);
			pw.print(json);
			pw.flush();
		} finally {
			pw.close();
		}
		return null;
	}
	/**
	 * 打开行政区域修改页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openEditArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String id = request.getParameter("id");
		//根据id查询行政区域信息
		AdministrativeArea area=areaService.findAreaInfoById(id);
		request.setAttribute("tgId", tgId);
		request.setAttribute("area", area);
		return "/WEB-INF/page/area/area_edit.jsp";
	}
	/**
	 * 修改行政区域信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String areaLevel = request.getParameter("areaLevel");
		String id = request.getParameter("id");
		String pid = request.getParameter("pid");
		String areaCode = request.getParameter("areaCode");
		String areaName = request.getParameter("areaName");
		String tgId = request.getParameter("tgId");
		AdministrativeArea area = new AdministrativeArea();
		area.setId(Integer.parseInt(id));
		area.setPid(Integer.parseInt(pid));
		area.setAreaName(areaName);
		area.setAreaCode(areaCode);
		area.setAreaLevel(areaLevel);
		
		areaService.editArea(area);
		request.setAttribute("tgId", tgId);
		return findAreaByPage(request,response);
	}
}
